package com.zit.java.controller;

import com.zit.java.pojo.Account;
import com.zit.java.service.login.LoginService;
import com.zit.java.util.object.AjaxDone;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import java.util.Random;
@Transactional
@Controller
@Api(tags = "/login")
@RequestMapping("login")
public class LoginController extends  BaseController {

    @Autowired
    private LoginService service;

    @RequestMapping("tologin")
    @ApiOperation(value = "login",notes = "到login页面")
    @ApiImplicitParams(
            @ApiImplicitParam(name ="username",value = "用户名",paramType = "String",required = true)

    )
    public String doLogin(String userName, String password, String verifyCode, Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        if(!StringUtils.hasLength(userName)){
            model.addAttribute("msg","请输入用户名!");
            return "../../login";
        }
        if(!StringUtils.hasLength(password)){
            model.addAttribute("msg","请输入密码!");
            return "../../login";
        }

        if(!StringUtils.hasLength(verifyCode)){
            model.addAttribute("msg","请输入验证码!");
            return "../../login";
        }
        HttpSession session = request.getSession();
        String sessionVerifycode = (String)session.getAttribute("verifyCode");
        if(!verifyCode.equalsIgnoreCase(sessionVerifycode)){
            model.addAttribute("msg","验证码错误!");
            return "../../login";
        }

        //去数据库查询用户信息
        Account acc = service.findByAccName(userName);
        if(ObjectUtils.isEmpty(acc)){
            model.addAttribute("userName",userName);
            model.addAttribute("msg","当前账号不存在!");

            return "../../login";
        }

        if(!password.equals(acc.getPassword())){
            model.addAttribute("msg","密码错误!");
            return "../../login";
        }
        //session
        session.setAttribute(SESSIONOBJ, acc);
        //获取servlet上下文对象
        ServletContext servletContext = request.getServletContext();
        //把session放进application里面
        servletContext.setAttribute(String.valueOf(acc.getId()),session);
        //设置登录的用户名
        model.addAttribute("username",acc.getName());
        return "main";
    }

    @RequestMapping("verifyCode")
    public void verifyCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int width = 80; // 图片的宽度
        int height = 30;// 图片的高度
        /*
         * 绘图
         */
        // step1,创建一个内存映像对象(画板)
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        // step2,获得画笔
        Graphics g = image.getGraphics();
        // step3,给笔上色
        Random r = new Random();
        g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
        // step4,给画板设置背景颜色
        g.fillRect(0, 0, width, height);
        // step5,绘制一个随机的字符串
        String characters="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        //String number = r.nextInt(99999) + "";
        String number = "";
        for(int i=1;i<=4;i++) {
            number+=characters.charAt(r.nextInt(62));
        }
        g.setColor(new Color(0, 0, 0));
        // new Font(字体,风格,大小)
        g.setFont(new Font(null, Font.ITALIC, 24));
        g.drawString(number, 5, 25);
        // step6,加一些干扰线
        for (int i = 0; i < 8; i++) {
            g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
            g.drawLine(r.nextInt(width), r.nextInt(height), r.nextInt(width), r.nextInt(height));
        }
        /*
         * 压缩图片并输出到客户端(浏览器)
         */
        request.getSession().setAttribute("verifyCode", number);
        response.setContentType("image/jpeg");
        OutputStream ops = response.getOutputStream();
        javax.imageio.ImageIO.write(image, "jpeg", ops);
        ops.close();
    }

    /**
     * 到修改密码
     * @return
     */
    @RequestMapping("toUpdatePwd")
    public String updatePas(Model model,HttpSession session,HttpServletRequest request,HttpServletResponse response){
        Account accUser =   (Account) session.getAttribute(SESSIONOBJ);
        model.addAttribute("acc",accUser);
        return "update/updatePwd";
    }

    /**
     * x修改密码
     * @param
     */
        @RequestMapping("updatePassword")

    public AjaxDone upaDataPass(String id
                ,String oldPassword
                ,String newPassword
                ,String againPassword
                ,HttpSession session
                ,HttpServletResponse response
        ,HttpServletRequest request){
            //后端校验
     Account accUser =   (Account) session.getAttribute(SESSIONOBJ);
     if (!accUser.getPassword().equals(oldPassword)){
         return new AjaxDone(300,"密码与服务器密码不一致!");
     }
     //判断密码不能为空
     if (ObjectUtils.isEmpty(oldPassword)){
         return  new AjaxDone(300,"密码不能为空!");
     }

     if (ObjectUtils.isEmpty(newPassword)){
         return  new AjaxDone(300,"密码不能为空!");
     }

     if (ObjectUtils.isEmpty(againPassword)){
         return  new AjaxDone(300,"密码不能为空!");
     }

     if (!newPassword.equals(againPassword)){
         return  new AjaxDone(302,"两次密码不一致,请重新输入!");
     }

     int rest =  service.updatePassword(newPassword,String.valueOf(accUser.getId()));
     if (rest>0){
         accUser.setPassword(newPassword);
         return new AjaxDone(200,"修改密码成功");
     }
        return null;
    }



    //将该方法织入到事务管理的aop中
    @Transactional(propagation = Propagation.REQUIRED)
    @RequestMapping("tojson")
    public void getJson(HttpServletResponse response){
        response.setContentType("text/json;charset=utf-8");
        try {
            PrintWriter out =  response.getWriter();
            out.print("{\n" +
                    "  \"homeInfo\": {\n" +
                    "    \"title\": \"首页\",\n" +
                    "    \"href\": \"login/homePage\"\n" +
                    "  },\n" +
                    "  \"logoInfo\": {\n" +
                    "    \"title\": \"LAYUI MINI\",\n" +
                    "    \"image\": \"images/logo.png\",\n" +
                    "    \"href\": \"\"\n" +
                    "  },\n" +
                    "  \"menuInfo\": [\n" +
                    "    {\n" +
                    "      \"title\": \"常规管理\",\n" +
                    "      \"icon\": \"fa fa-address-book\",\n" +
                    "      \"href\": \"\",\n" +
                    "      \"target\": \"_self\",\n" +
                    "      \"child\": [\n" +
                    "        {\n" +
                    "          \"title\": \"主页模板\",\n" +
                    "          \"href\": \"\",\n" +
                    "          \"icon\": \"fa fa-home\",\n" +
                    "          \"target\": \"_self\",\n" +
                    "          \"child\": [\n" +
                    "            {\n" +
                    "              \"title\": \"主页一\",\n" +
                    "              \"href\": \"page/welcome-1.html\",\n" +
                    "              \"icon\": \"fa fa-tachometer\",\n" +
                    "              \"target\": \"_self\"\n" +
                    "            },\n" +
                    "            {\n" +
                    "              \"title\": \"主页二\",\n" +
                    "              \"href\": \"page/welcome-2.html\",\n" +
                    "              \"icon\": \"fa fa-tachometer\",\n" +
                    "              \"target\": \"_self\"\n" +
                    "            },\n" +
                    "            {\n" +
                    "              \"title\": \"主页三\",\n" +
                    "              \"href\": \"page/welcome-3.html\",\n" +
                    "              \"icon\": \"fa fa-tachometer\",\n" +
                    "              \"target\": \"_self\"\n" +
                    "            }\n" +
                    "          ]\n" +
                    "        },\n" +
                    "        {\n" +
                    "          \"title\": \"菜单管理\",\n" +
                    "          \"href\": \"menu/toMenu\",\n" +
                    "          \"icon\": \"fa fa-window-maximize\",\n" +
                    "          \"target\": \"_self\"\n" +
                    "        },\n" +
                    "        {\n" +
                    "          \"title\": \"系统设置\",\n" +
                    "          \"href\": \"page/setting.html\",\n" +
                    "          \"icon\": \"fa fa-gears\",\n" +
                    "          \"target\": \"_self\"\n" +
                    "        },\n" +
                    "        {\n" +
                    "          \"title\": \"表格示例\",\n" +
                    "          \"href\": \"page/table.html\",\n" +
                    "          \"icon\": \"fa fa-file-text\",\n" +
                    "          \"target\": \"_self\"\n" +
                    "        },\n" +
                    "        {\n" +
                    "          \"title\": \"表单示例\",\n" +
                    "          \"href\": \"\",\n" +
                    "          \"icon\": \"fa fa-calendar\",\n" +
                    "          \"target\": \"_self\",\n" +
                    "          \"child\": [\n" +
                    "            {\n" +
                    "              \"title\": \"普通表单\",\n" +
                    "              \"href\": \"page/form.html\",\n" +
                    "              \"icon\": \"fa fa-list-alt\",\n" +
                    "              \"target\": \"_self\"\n" +
                    "            },\n" +
                    "            {\n" +
                    "              \"title\": \"分步表单\",\n" +
                    "              \"href\": \"page/form-step.html\",\n" +
                    "              \"icon\": \"fa fa-navicon\",\n" +
                    "              \"target\": \"_self\"\n" +
                    "            }\n" +
                    "          ]\n" +
                    "        },\n" +
                    "        {\n" +
                    "          \"title\": \"登录模板\",\n" +
                    "          \"href\": \"\",\n" +
                    "          \"icon\": \"fa fa-flag-o\",\n" +
                    "          \"target\": \"_self\",\n" +
                    "          \"child\": [\n" +
                    "            {\n" +
                    "              \"title\": \"登录-1\",\n" +
                    "              \"href\": \"page/login-1.html\",\n" +
                    "              \"icon\": \"fa fa-stumbleupon-circle\",\n" +
                    "              \"target\": \"_blank\"\n" +
                    "            },\n" +
                    "            {\n" +
                    "              \"title\": \"登录-2\",\n" +
                    "              \"href\": \"page/login-2.html\",\n" +
                    "              \"icon\": \"fa fa-viacoin\",\n" +
                    "              \"target\": \"_blank\"\n" +
                    "            },\n" +
                    "            {\n" +
                    "              \"title\": \"登录-3\",\n" +
                    "              \"href\": \"page/login-3.html\",\n" +
                    "              \"icon\": \"fa fa-tags\",\n" +
                    "              \"target\": \"_blank\"\n" +
                    "            }\n" +
                    "          ]\n" +
                    "        },\n" +
                    "        {\n" +
                    "          \"title\": \"异常页面\",\n" +
                    "          \"href\": \"\",\n" +
                    "          \"icon\": \"fa fa-home\",\n" +
                    "          \"target\": \"_self\",\n" +
                    "          \"child\": [\n" +
                    "            {\n" +
                    "              \"title\": \"404页面\",\n" +
                    "              \"href\": \"page/404.html\",\n" +
                    "              \"icon\": \"fa fa-hourglass-end\",\n" +
                    "              \"target\": \"_self\"\n" +
                    "            }\n" +
                    "          ]\n" +
                    "        },\n" +
                    "        {\n" +
                    "          \"title\": \"其它界面\",\n" +
                    "          \"href\": \"\",\n" +
                    "          \"icon\": \"fa fa-snowflake-o\",\n" +
                    "          \"target\": \"\",\n" +
                    "          \"child\": [\n" +
                    "            {\n" +
                    "              \"title\": \"按钮示例\",\n" +
                    "              \"href\": \"page/button.html\",\n" +
                    "              \"icon\": \"fa fa-snowflake-o\",\n" +
                    "              \"target\": \"_self\"\n" +
                    "            },\n" +
                    "            {\n" +
                    "              \"title\": \"弹出层\",\n" +
                    "              \"href\": \"page/layer.html\",\n" +
                    "              \"icon\": \"fa fa-shield\",\n" +
                    "              \"target\": \"_self\"\n" +
                    "            }\n" +
                    "          ]\n" +
                    "        }\n" +
                    "      ]\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"title\": \"组件管理\",\n" +
                    "      \"icon\": \"fa fa-lemon-o\",\n" +
                    "      \"href\": \"\",\n" +
                    "      \"target\": \"_self\",\n" +
                    "      \"child\": [\n" +
                    "        {\n" +
                    "          \"title\": \"图标列表\",\n" +
                    "          \"href\": \"page/icon.html\",\n" +
                    "          \"icon\": \"fa fa-dot-circle-o\",\n" +
                    "          \"target\": \"_self\"\n" +
                    "        },\n" +
                    "        {\n" +
                    "          \"title\": \"图标选择\",\n" +
                    "          \"href\": \"page/icon-picker.html\",\n" +
                    "          \"icon\": \"fa fa-adn\",\n" +
                    "          \"target\": \"_self\"\n" +
                    "        },\n" +
                    "        {\n" +
                    "          \"title\": \"颜色选择\",\n" +
                    "          \"href\": \"page/color-select.html\",\n" +
                    "          \"icon\": \"fa fa-dashboard\",\n" +
                    "          \"target\": \"_self\"\n" +
                    "        },\n" +
                    "        {\n" +
                    "          \"title\": \"下拉选择\",\n" +
                    "          \"href\": \"page/table-select.html\",\n" +
                    "          \"icon\": \"fa fa-angle-double-down\",\n" +
                    "          \"target\": \"_self\"\n" +
                    "        },\n" +
                    "        {\n" +
                    "          \"title\": \"文件上传\",\n" +
                    "          \"href\": \"page/upload.html\",\n" +
                    "          \"icon\": \"fa fa-arrow-up\",\n" +
                    "          \"target\": \"_self\"\n" +
                    "        },\n" +
                    "        {\n" +
                    "          \"title\": \"富文本编辑器\",\n" +
                    "          \"href\": \"page/editor.html\",\n" +
                    "          \"icon\": \"fa fa-edit\",\n" +
                    "          \"target\": \"_self\"\n" +
                    "        },\n" +
                    "        {\n" +
                    "          \"title\": \"省市县区选择器\",\n" +
                    "          \"href\": \"page/area.html\",\n" +
                    "          \"icon\": \"fa fa-rocket\",\n" +
                    "          \"target\": \"_self\"\n" +
                    "        }\n" +
                    "      ]\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"title\": \"其它管理\",\n" +
                    "      \"icon\": \"fa fa-slideshare\",\n" +
                    "      \"href\": \"\",\n" +
                    "      \"target\": \"_self\",\n" +
                    "      \"child\": [\n" +
                    "        {\n" +
                    "          \"title\": \"多级菜单\",\n" +
                    "          \"href\": \"\",\n" +
                    "          \"icon\": \"fa fa-meetup\",\n" +
                    "          \"target\": \"\",\n" +
                    "          \"child\": [\n" +
                    "            {\n" +
                    "              \"title\": \"按钮1\",\n" +
                    "              \"href\": \"page/button.html?v=1\",\n" +
                    "              \"icon\": \"fa fa-calendar\",\n" +
                    "              \"target\": \"_self\",\n" +
                    "              \"child\": [\n" +
                    "                {\n" +
                    "                  \"title\": \"按钮2\",\n" +
                    "                  \"href\": \"page/button.html?v=2\",\n" +
                    "                  \"icon\": \"fa fa-snowflake-o\",\n" +
                    "                  \"target\": \"_self\",\n" +
                    "                  \"child\": [\n" +
                    "                    {\n" +
                    "                      \"title\": \"按钮3\",\n" +
                    "                      \"href\": \"page/button.html?v=3\",\n" +
                    "                      \"icon\": \"fa fa-snowflake-o\",\n" +
                    "                      \"target\": \"_self\"\n" +
                    "                    },\n" +
                    "                    {\n" +
                    "                      \"title\": \"表单4\",\n" +
                    "                      \"href\": \"page/form.html?v=1\",\n" +
                    "                      \"icon\": \"fa fa-calendar\",\n" +
                    "                      \"target\": \"_self\"\n" +
                    "                    }\n" +
                    "                  ]\n" +
                    "                }\n" +
                    "              ]\n" +
                    "            }\n" +
                    "          ]\n" +
                    "        },\n" +
                    "        {\n" +
                    "          \"title\": \"失效菜单\",\n" +
                    "          \"href\": \"page/error.html\",\n" +
                    "          \"icon\": \"fa fa-superpowers\",\n" +
                    "          \"target\": \"_self\"\n" +
                    "        }\n" +
                    "      ]\n" +
                    "    }\n" +
                    "  ]\n" +
                    "}");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    @RequestMapping("tojson2")
    public void getjj(HttpServletResponse response) throws IOException {
        PrintWriter writer = response.getWriter();
        response.setContentType("text/json;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        writer.println("{\n" +
                "  \"code\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"count\": 1000,\n" +
                "  \"data\": [\n" +
                "    {\n" +
                "      \"id\": 10000,\n" +
                "      \"username\": \"user-0\",\n" +
                "      \"sex\": \"女\",\n" +
                "      \"city\": \"城市-0\",\n" +
                "      \"sign\": \"签名-0\",\n" +
                "      \"experience\": 255,\n" +
                "      \"logins\": 24,\n" +
                "      \"wealth\": 82830700,\n" +
                "      \"classify\": \"作家\",\n" +
                "      \"score\": 57\n" +
                "    },\n" +
                "    {\n" +
                "      \"id\": 10001,\n" +
                "      \"username\": \"user-1\",\n" +
                "      \"sex\": \"男\",\n" +
                "      \"city\": \"城市-1\",\n" +
                "      \"sign\": \"签名-1\",\n" +
                "      \"experience\": 884,\n" +
                "      \"logins\": 58,\n" +
                "      \"wealth\": 64928690,\n" +
                "      \"classify\": \"词人\",\n" +
                "      \"score\": 27\n" +
                "    },\n" +
                "    {\n" +
                "      \"id\": 10002,\n" +
                "      \"username\": \"user-2\",\n" +
                "      \"sex\": \"女\",\n" +
                "      \"city\": \"城市-2\",\n" +
                "      \"sign\": \"签名-2\",\n" +
                "      \"experience\": 650,\n" +
                "      \"logins\": 77,\n" +
                "      \"wealth\": 6298078,\n" +
                "      \"classify\": \"酱油\",\n" +
                "      \"score\": 31\n" +
                "    },\n" +
                "    {\n" +
                "      \"id\": 10003,\n" +
                "      \"username\": \"user-3\",\n" +
                "      \"sex\": \"女\",\n" +
                "      \"city\": \"城市-3\",\n" +
                "      \"sign\": \"签名-3\",\n" +
                "      \"experience\": 362,\n" +
                "      \"logins\": 157,\n" +
                "      \"wealth\": 37117017,\n" +
                "      \"classify\": \"诗人\",\n" +
                "      \"score\": 68\n" +
                "    },\n" +
                "    {\n" +
                "      \"id\": 10004,\n" +
                "      \"username\": \"user-4\",\n" +
                "      \"sex\": \"男\",\n" +
                "      \"city\": \"城市-4\",\n" +
                "      \"sign\": \"签名-4\",\n" +
                "      \"experience\": 807,\n" +
                "      \"logins\": 51,\n" +
                "      \"wealth\": 76263262,\n" +
                "      \"classify\": \"作家\",\n" +
                "      \"score\": 6\n" +
                "    },\n" +
                "    {\n" +
                "      \"id\": 10005,\n" +
                "      \"username\": \"user-5\",\n" +
                "      \"sex\": \"女\",\n" +
                "      \"city\": \"城市-5\",\n" +
                "      \"sign\": \"签名-5\",\n" +
                "      \"experience\": 173,\n" +
                "      \"logins\": 68,\n" +
                "      \"wealth\": 60344147,\n" +
                "      \"classify\": \"作家\",\n" +
                "      \"score\": 87\n" +
                "    },\n" +
                "    {\n" +
                "      \"id\": 10006,\n" +
                "      \"username\": \"user-6\",\n" +
                "      \"sex\": \"女\",\n" +
                "      \"city\": \"城市-6\",\n" +
                "      \"sign\": \"签名-6\",\n" +
                "      \"experience\": 982,\n" +
                "      \"logins\": 37,\n" +
                "      \"wealth\": 57768166,\n" +
                "      \"classify\": \"作家\",\n" +
                "      \"score\": 34\n" +
                "    },\n" +
                "    {\n" +
                "      \"id\": 10007,\n" +
                "      \"username\": \"user-7\",\n" +
                "      \"sex\": \"男\",\n" +
                "      \"city\": \"城市-7\",\n" +
                "      \"sign\": \"签名-7\",\n" +
                "      \"experience\": 727,\n" +
                "      \"logins\": 150,\n" +
                "      \"wealth\": 82030578,\n" +
                "      \"classify\": \"作家\",\n" +
                "      \"score\": 28\n" +
                "    },\n" +
                "    {\n" +
                "      \"id\": 10008,\n" +
                "      \"username\": \"user-8\",\n" +
                "      \"sex\": \"男\",\n" +
                "      \"city\": \"城市-8\",\n" +
                "      \"sign\": \"签名-8\",\n" +
                "      \"experience\": 951,\n" +
                "      \"logins\": 133,\n" +
                "      \"wealth\": 16503371,\n" +
                "      \"classify\": \"词人\",\n" +
                "      \"score\": 14\n" +
                "    },\n" +
                "    {\n" +
                "      \"id\": 10009,\n" +
                "      \"username\": \"user-9\",\n" +
                "      \"sex\": \"女\",\n" +
                "      \"city\": \"城市-9\",\n" +
                "      \"sign\": \"签名-9\",\n" +
                "      \"experience\": 484,\n" +
                "      \"logins\": 25,\n" +
                "      \"wealth\": 86801934,\n" +
                "      \"classify\": \"词人\",\n" +
                "      \"score\": 75\n" +
                "    }\n" +
                "  ]\n" +
                "}");
    }
    @RequestMapping("homePage")
    public String homePage(){
        return "home/index";
    }


    /**
     * 把json数据封装到map中
     * 查询菜单信息
     * @return
     */

    @RequestMapping("init")
    @ResponseBody
    public Map<String,Object> menuInfo(HttpServletRequest request,Model model){
        //获取当前登录用户的Id
       Account account =   getLoginAcc(request);
       if (account.getId()==3){
           model.addAttribute("admin",1);
       }


       int userId = (int) account.getId();
       //获取当前登录用户的菜单信息;
        Map<String,Object> mapInfo =  service.queryMenu1(userId);
        //查询到的item_code和value1  放入session里面,获取当前用户能进入的页面的按钮信息
        Map<String,Integer> allRightsBtn =  service.queryAllRightsBtn(userId);
        //获取session对象,把session对象放到jsp页面中
        request.getSession().setAttribute("btnRights",allRightsBtn);
        return  mapInfo;
    }


    /**
     * 去注册页面
     * @return
     */
    @RequestMapping("toRegister")
    public String toRegist(){
        return "register/index";
    }


    @RequestMapping("Material")
    public String toMaterial(HttpServletRequest request,Model model){
      Account account =   getLoginAcc(request);
      model.addAttribute("acc",account);
        return "login/material";
        }

    /**
     * 注册账户
     * @param account
     * @return
     */
    @RequestMapping("register")
    @ResponseBody
    public AjaxDone registerUser(Account account){
        List<String> accounts = service.queryAllName();
        for (String name : accounts){
            if (account.getName().equals(name)){
                return new AjaxDone(350,"您注册的用户名已经存在!");
            }
        }

        int rst  =   service.insertUser(account);
        if (rst>0){
            return new AjaxDone(200,"您已经注册成功!");
        }
        return new AjaxDone(300,"服务器繁忙");
    }

    /**
     * 修改个人信息
     * @param name
     * @param sid
     * @param phone
     * @param accid
     * @return
     */
        @RequestMapping("updmeterial")
        @ResponseBody
        public AjaxDone updMeterial(@RequestParam("name") String name,
                                    @RequestParam("sid") String sid,
                                    @RequestParam("phone") String phone,
                                    @RequestParam("accid") String accid)
        {
             int rst =    service.updateMeterial(name,sid,phone,accid);
            if (rst>0){
                return new AjaxDone(200,"修改成功!");
            }
                 return new AjaxDone(300,"服务器繁忙!");
        }


    /**
     * 到忘记密码页面
     * @return
     */
        @RequestMapping("fogetpassword")
        public  String toForgetPassword(){
            return "forgetpwd/forgetpsw";
        }

    /**
     * 找回密码
     *
     * 有bug
     * @param
     * @return
     */
        @RequestMapping("retrieve")
        @ResponseBody
        public AjaxDone retrievePassword(@RequestParam("sid")String sid
                ,@RequestParam("pid")String phoneId,@RequestParam("username")String username){

            if (!StringUtils.hasLength(username)) {
                return new AjaxDone(350, "用户名不能为空");
            }

            List<String> pasword = service.querySidAndPhoneId(sid,phoneId);
            for (String psd : pasword) {
                  if (StringUtils.hasLength(psd)){
                    return new AjaxDone(200, "找回成功",psd);
                }else {
                      return new AjaxDone(300,"服务器繁忙");
                  }

            }
                    return new AjaxDone(300,"服务器繁忙");
        }



}


